package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.sgb.jicai.view.fragment.ChildForDemandJiCaiFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

/**
 * Time:2021/5/16
 * AUTHOR:F_Q
 * Description:
 */
public class ChildForDemandJiCaiModel extends ViewModel {
    private ChildForDemandJiCaiFragment fragment;
    private int current;

    //集采list
    public void getJiCaiList(JiCaiHomeListRequestBean map, boolean isLoadMore) {
        int currentPage = isLoadMore ? current + 1 : 1;
        map.setLimit(10);
        map.setCurrent(currentPage);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
       JiCaiBaseApi.getApi().getJiCaiLists(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<CentralizedMiningEntity>>> data) {
                current = data.getData().getCurrentPage();
                int total = data.getData().getTotal();
                fragment.showList(data.getData().getList(),isLoadMore,current >= total);
            }
        });
    }

    //获取详情信息
    public void getH5Url(String no) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting("collection").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    SharedPreferenceUtil.init(fragment.getActivity());
                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                    String url = data.getData().getUrl() + "?id=" + no + "&type=0" + "&token=" + token;
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    public void setActivity(ChildForDemandJiCaiFragment childForDemandJiCaiFragment) {
        this.fragment = childForDemandJiCaiFragment;
    }
}
